package com.ruoyi.web.controller.system;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.detectUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HistoryInfo;
import com.ruoyi.system.service.IHistoryInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 历史信息Controller
 * 
 * @author YDB
 * @date 2022-04-05
 */
@RestController
@RequestMapping("/system/historyInfo")
public class HistoryInfoController extends BaseController
{
    @Autowired
    private IHistoryInfoService historyInfoService;

    /**
     * 查询历史信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HistoryInfo historyInfo)
    {
        startPage();
        List<HistoryInfo> list = historyInfoService.selectHistoryInfoList(historyInfo);
        return getDataTable(list);
    }

    /**
     * 导出历史信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:historyInfo:export')")
    @Log(title = "历史信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HistoryInfo historyInfo)
    {
        List<HistoryInfo> list = historyInfoService.selectHistoryInfoList(historyInfo);
        ExcelUtil<HistoryInfo> util = new ExcelUtil<HistoryInfo>(HistoryInfo.class);
        util.exportExcel(response, list, "历史信息数据");
    }

    /**
     * 获取历史信息详细信息
     */
    @GetMapping(value = "/{historyInfoId}")
    public AjaxResult getInfo(@PathVariable("historyInfoId") Long historyInfoId)
    {
        return AjaxResult.success(historyInfoService.selectHistoryInfoByHistoryInfoId(historyInfoId));
    }

    /**
     * 新增历史信息
     */
    @PreAuthorize("@ss.hasPermi('system:historyInfo:add')")
    @Log(title = "历史信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HistoryInfo historyInfo)
    {
        historyInfo.setCreaterID(getUserId());
        historyInfo.setCreateName(getUsername());
        return toAjax(historyInfoService.insertHistoryInfo(historyInfo));
    }

    /**
     * 修改历史信息
     */
    @PreAuthorize("@ss.hasPermi('system:historyInfo:edit')")
    @Log(title = "历史信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HistoryInfo historyInfo)
    {
        return toAjax(historyInfoService.updateHistoryInfo(historyInfo));
    }

    /**
     * 删除历史信息
     */
    @Log(title = "历史信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{historyInfoIds}")
    public AjaxResult remove(@PathVariable Long[] historyInfoIds)
    {
        return toAjax(historyInfoService.deleteHistoryInfoByHistoryInfoIds(historyInfoIds));
    }

    /**
     * 检测
     */
    @PostMapping("detect")
    @Log(title = "算法识别", businessType = BusinessType.INSERT)
    public AjaxResult detect(@RequestBody List<Map<String,String>> data)
    {
        //删除垃圾文件
        String path=RuoYiConfig.getUploadPath();
        AsyncManager.me().execute(AsyncFactory.delTargetDirFile(path));
        if(data.get(0).get("typeFlag").equals("1")){
            return historyInfoService.bhjc(data,getUserId(),getUsername());
        }else if(data.get(0).get("typeFlag").equals("2")){
            return historyInfoService.mbtq(data,getUserId(),getUsername());
        }else if(data.get(0).get("typeFlag").equals("3")){
            return historyInfoService.mbjc(data,getUserId(),getUsername());
        }else if(data.get(0).get("typeFlag").equals("4")){
            return historyInfoService.dwfl(data,getUserId(),getUsername());
        }else if(data.get(0).get("typeFlag").equals("5")){
            return historyInfoService.cljc(data,getUserId(),getUsername());
        }else if(data.get(0).get("typeFlag").equals("6")){
            return historyInfoService.yyfg(data,getUserId(),getUsername());
        }else if(data.get(0).get("typeFlag").equals("7")){
            return historyInfoService.cjfl(data,getUserId(),getUsername());
        }
        return AjaxResult.success();
    }

    /**
     * 检测类别统计
     */
    @GetMapping("detectTypeInfo")
    public AjaxResult detectTypeInfo(){
        return historyInfoService.selectDetectTypeInfo();
    }

    /**
     * 上周检测统计
     */
    @GetMapping("detectNum")
    public AjaxResult detectNum(){
        return historyInfoService.selectDetectNum();
    }
}
